package chpt05;
public class Car {
	private String name;    // 汽车型号
	private int speed = 0;
	private Engine engine;
	private static int MAX_SPEED = 200;
	
	public Car() {}

	public Car(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getSpeed() {
		return speed;
	}

	public boolean speedUp() {
		boolean flag = engine.speedUp();
		if (flag) {
			System.out.println("加速成功! Speed = " +speed);
		} else {
			System.out.println("加速失败! Speed = " +speed);
		}
		return flag;
	}

	public boolean speedDown() {
		return engine.speedDown();
	}

	public class Engine {
		private String name;    // 引擎型号
		private int horsepower; // 马力

		public Engine(String name, int horsepower) {
			this.name = name;
			this.horsepower = horsepower;
		}

		public int getHorsepower() {
			return horsepower;
		}
		
		public void show() {
			System.out.printf("我是%s引擎，拥有%d匹马力，搭载在%s车上。%n",
					name, horsepower, Car.this.name);
		}

		public boolean speedUp() {
			if (speed + 1 > Car.MAX_SPEED)
				return false;
			speed++;
			return true;
		}

		public boolean speedDown() {
			if (speed - 1 < 0)
				return false;
			speed--;
			return true;
		}
	}

	public void start(Engine engine) {
		this.engine = engine;
		System.out.println("Car-" + name + 
				", 使用 " + engine.getHorsepower()+"匹马力启动！");
	}
}